/*
 * Background Flickr: Copyright (C) 2009  Craig McNicholas
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.ddw.app.bflickr.flickr.api.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.ddw.app.bflickr.logging.Log;
import com.ddw.app.bflickr.logging.LogFactory;

/**
 * Class to handle email address values and validating them
 * @author craig
 *
 */
public class EmailAddress {

	private Log log = LogFactory.createInstance(this.getClass());
	private String email = null;
	
	/**
	 * Constructs a new email address instance
	 * @param email the email address to use
	 * @throws IllegalArgumentException when the email is not in a valid format, <i>email addresses cannot be invalid</i>
	 */
	public EmailAddress(String email) throws IllegalArgumentException {
		if(email != null) {
			if(email.trim().length() != 0 && validateEmailAddress(email)) {
				this.email = email;
			} else {
				log.error("The email address was not in the correct format.");
				throw new IllegalArgumentException("The email address was not in the correct format.");
			}
		} else {
			log.error("The email address cannto be null.");
			throw new IllegalArgumentException("The email address cannto be null.");
		}
	}
	
	/**
	 * Getter of the email address
	 * @return the email address string
	 */
	public String getEmailAddress() {
		return this.email;
	}
	
	@Override
	public String toString() {
		return this.email;
	}
	
	/**
	 * The expression to validate email addresses against. Making it final and static 
	 * provides performance benefits.
	 */
	private final static String expression = "^\\w+[\\+\\.\\w-]*@([\\w-]+\\.)*\\w+[\\w-]*\\.([a-z]{2,4}|\\d+)$";
	
	/**
	 * The compiled patten to validate emails against. Making it final and static 
	 * provides performance benefits by allowing the validator to not have to compile costly 
	 * expressions over and over.
	 */
	private final static Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
	
	/**
	 * The pattern matcher used to validate emails against. Making it static 
	 * provides performance benefits by allowing the validator to reuse the matcher.
	 */
	private static Matcher matcher = null;
	
	/**
	 * Validates an email address against a regular expression
	 * @param email the email address to validate
	 * @return a boolean flag indicating whether the email is valid
	 */
	public static boolean validateEmailAddress(String email) {
		if(matcher == null) {
			matcher = pattern.matcher(email);
		} else { // Reuse matcher if instance is valid
			matcher.reset(email);
		}
		return matcher.matches();
	}
	
}
